package com.example.xiaoyuan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xiaoyuan.pojo.Admin;
import com.example.xiaoyuan.pojo.Teacher;
import com.example.xiaoyuan.service.TeacherService;
import com.example.xiaoyuan.utils.MD5;
import com.example.xiaoyuan.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "教师控制器")
@RestController
@RequestMapping("/sms/teacherController")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 删除教师
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(@RequestBody List<Integer> ids){


        teacherService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 添加和修改教师
     * @param teacher
     * @return
     */
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(@RequestBody Teacher teacher){

        //若果是新增要对密码进行加密
        Integer id = teacher.getId();
        if (id == null || 0 ==id){
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        boolean b = teacherService.saveOrUpdate(teacher);

        return Result.ok();
    }



    /**
     * 分页查询教师信息
     * @param pageNo
     * @param pageSize
     * @param teacher
     * @return
     */
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeachers (
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            Teacher teacher
    ){
        Page<Teacher> teacherPage = new Page<Teacher>(pageNo, pageSize);

        IPage<Teacher> iPage = teacherService.getTeacherByOpr(teacherPage, teacher);
        return Result.ok(iPage);
    }

}
